package com.alcatel_lucent.osp.action;

import com.alcatel_lucent.osp.model.Category;
import com.alcatel_lucent.osp.model.Type;
import com.alcatel_lucent.osp.service.DaoService;
import com.alcatel_lucent.osp.service.JSONService;
import com.alcatel_lucent.osp.service.JSONServiceImpl;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.intellij.lang.annotations.Language;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Guoshun Wu
 * Date: 13-9-7
 * Time: 下午9:13
 */
public class CreateNewRequestAction extends BaseAction {

    private Long category;
    @Autowired
    private DaoService dao;

    public Long getCategory() {
        return category;
    }

    public void setCategory(Long category) {
        this.category = category;
    }


    public Collection<Category> getCategories() {
        return dao.retrieve("from Category order by name");
    }

    public Collection<Type> getTypes() {
        if (category != null) {
            @Language("HQL")String hql = "select t from Category c join c.types t where c.id=:cid order by t.id";
            Map<String,Object> params =new HashMap<String,Object>();
            params.put("cid", category);
            return dao.retrieve(hql, params);
        } else {
            return new ArrayList<Type>();
        }
    }


    @Override
    public String execute() throws Exception {
        log.info("in create new request action...");
        return SUCCESS;
    }
}
